let path = require('path')
const webpack = require('webpack')
const environment = require('./build/env.development')
const CompressionPlugin = require('compression-webpack-plugin')
const url = require('./build/url')
function resolve(dir) {
  return path.join(__dirname, dir)
}

// 获取打包命令后面的参数 -prod -demo -sit
const command = JSON.parse(process.env.npm_config_argv)['original']
let ORIGINAL = '/digital-economy-library'
if (command[1] === 'build' && command[2]) {
  ORIGINAL = '/'
}
module.exports = {
  // publicPath: ORIGINAL,
  publicPath: '/',
  outputDir: 'html',
  productionSourceMap: false,
  configureWebpack: {
    plugins: [
      new webpack.DefinePlugin({
        'process.env.STAGE': JSON.stringify(environment.stage),
        'process.env.LOCAL_URL': JSON.stringify(environment.localUrl)
      })
    ],
    // 使用cdn
    externals: {
      // 'element-ui': 'ELEMENT',
      // 'echarts': 'echarts',
      // 'vue': 'Vue',
      // 'vuex': 'Vuex',
      // 'axios': 'axios',
      // 'vue-router': 'VueRouter'
    }
  },
  chainWebpack: config => {
    // config.plugin('webpack-bundle-analyzer')
    //   .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin)
    config.resolve.alias
      .set('@', resolve('src'))
      .set('lang', resolve('src/lang'))
      .set('utils', resolve('src/utils'))
      .set('mixins', resolve('src/mixins'))
      .set('assets', resolve('src/assets'))
      .set('mixins', resolve('src/mixins'))
      .set('columns', resolve('src/columns'))
      .set('components', resolve('src/components'))
    // 开启js、css压缩
    // if (process.env.NODE_ENV === 'production') {
    //   config.plugin('compressionPlugin')
    //     .use(new CompressionPlugin({
    //       test:/\.js$|\.css/, // 匹配文件名
    //       threshold: 1024, // 对超过1M的数据压缩
    //       deleteOriginalAssets: true // 不删除源文件
    //     }))
    // }
  },
  devServer: {
    // 本地白名单
    allowedHosts: [
      'digital-economy-library.com'
    ],
    port: 15012,
    proxy: {
      '/proxy': {
        target: 'http://digitaleconomylib.com/',
        pathRewrite: { '^/proxy': '' },
        changeOrigin: true,
        secure: false
      }
    }
  },
  css: {
    loaderOptions: {
      sass: {
        prependData: '@import "@/assets/styles/mixins.scss";@import "@/assets/styles/variable.scss";'
      }
    }
  }
}
